<template>
  <div class="table">
    <BaseTable
      ref="table"
      rowKey="id"
      :scroll="{ y: y }"
      :loading="loading"
      :columns="columns"
      :dataSource="list"
      :hideSelect="false"
      :expandedRenderCxt="false"
      :expandedRowKeys.sync="expandedRowKeys"
      :selectedRowKeys="selectedRowKeys"
      @deleteRows="handleDelete"
      @selectChange="selectChange"
      @change="getList"
    >
    </BaseTable>
  </div>
</template>

<script>
import BaseTable from "./BaseTable";
import httpApi from "@/api/comment";

export default {
  components: {
    BaseTable
  },
  props: {
    queryName: {
      type: String,
      required: true,
      default: ""
    },
    type: {
      required: true,
      default: undefined
    },
    y: {
      type: Number,
      required: true,
      default: 0
    }
  },
  data() {
    return {
      loading: false,
      time: -1,
      list: [],
      selectedRowKeys: [],
      expandedRowKeys: [],
      columns: [
        {
          title: "姓名",
          dataIndex: "name"
        },
        {
          title: "手机号",
          dataIndex: "userMobile"
        },
        {
          title: "身份证号",
          dataIndex: "idCard"
        },
        {
          title: "昵称",
          dataIndex: "userName"
        },
        {
          title: "类型",
          dataIndex: "userType",
          customRender: (data) => {
            return data;
          }
        },
        {
          title: "发布时间",
          dataIndex: "createTime"
        },
        {
          title: "操作",
          width: 160,
          customRender: (v, row) => {
            return (
              <div class="operate-td">
                <a onClick={() => this.handlePass(row, 1)}>通过</a>
                <a-divider type="vertical" />
                <a onClick={() => this.handlePass(row, 2)}>不通过</a>
              </div>
            );
          }
        }
      ]
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    onSearch() {
      clearTimeout(this.time);
      this.time = setTimeout(() => {
        this.getList(1);
      }, 600);
    },
    getList(current) {
      this.loading = true;
      if (current === 1) {
        this.$refs.table.pageInfo.current = 1;
      }
      httpApi
        .waitAuditSearch({
          queryName: this.queryName,
          type: this.type,
          pageNo: this.$refs.table.pageInfo.current,
          pageSize: this.$refs.table.pageInfo.pageSize
        })
        .then((res) => {
          this.loading = false;
          if (res.retcode === 0) {
            this.list = res.data.records || [];
            if (this.$refs.table) {
              this.$refs.table.pageInfo.total = res.data.total;
            }
            this.$nextTick(() => {
              this.expandedRowKeys = this.list.map((node) => node.id);
            });
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
          this.loading = false;
        });
    },
    handleDelete(row) {
      let title = "";
      let content = "";
      let ids = [];
      if (row) {
        ids = [row.id];
        title = "删除：";
        content = "是否删除本条评论？";
      } else {
        ids = this.selectedRowKeys;
        title = "批量删除：";
        content = `是否删除已选中的 ${ids.length} 个评论？`;
      }

      this.$confirm({
        title,
        content,
        onOk: () => {
          httpApi
            .deleteRows(ids)
            .then((res) => {
              if (res.retcode === 0) {
                this.$message.success("删除成功！");
                this.getList();
              } else {
                this.$message.error(res.msg || "删除失败！");
              }
            })
            .catch((err) => {
              this.$message.error(err.msg || "删除失败！");
            });
        },
        onCancel: () => {
          console.log("已取消删除操作");
        }
      });
    },
    handlePass(row, autitResult) {
      //审批
      let title = autitResult === 1 ? "通过" : "不通过";
      let content =
        autitResult === 1
          ? "是否确认审批通过？"
          : "是否确认审批不通过？不通过系统将自动删除该评论";

      this.$confirm({
        title,
        content,
        onOk: () => {
          this.auditInter(row.id, autitResult);
        },
        onCancel: () => {}
      });
    },
    auditInter(id, autitResult) {
      this.loading = true;
      httpApi
        .audit({
          id: id,
          autitResult
        })
        .then((res) => {
          this.loading = false;
          if (res.retcode === 0) {
            this.$message.success("操作成功!");
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
          this.loading = false;
        });
    },
    selectChange(selectedKeys) {
      this.selectedRowKeys = selectedKeys;
    }
  }
};
</script>

<style lang="less" scoped></style>
